import pytest
from fastapi.testclient import TestClient
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from sqlalchemy.pool import StaticPool

from app.database import Base, get_db
from app.main import app
from app.config import settings
from app.models.models import Admin, Student, Building, Room, Interest

# 使用SQLite内存数据库进行测试
SQLALCHEMY_DATABASE_URL = "sqlite://"

engine = create_engine(
    SQLALCHEMY_DATABASE_URL,
    connect_args={"check_same_thread": False},
    poolclass=StaticPool,
)
TestingSessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)

@pytest.fixture(scope="function")
def db():
    """每个测试函数获取一个新的数据库会话"""
    Base.metadata.create_all(bind=engine)
    db = TestingSessionLocal()
    try:
        yield db
    finally:
        db.close()
        Base.metadata.drop_all(bind=engine)

@pytest.fixture(scope="function")
def client(db):
    """测试客户端"""
    def override_get_db():
        try:
            yield db
        finally:
            pass
    
    app.dependency_overrides[get_db] = override_get_db
    yield TestClient(app)
    app.dependency_overrides.clear()

@pytest.fixture
def super_admin(db):
    """创建超级管理员"""
    admin = Admin(
        username="superadmin",
        email="super@example.com",
        hashed_password="$2b$12$LQv3c1yqBWVHxkd0LHAkCOYz6TtxMQJqhN8/LewKrNYZGpk1hPEUy",  # password: test123
        is_super_admin=True,
        is_active=True
    )
    db.add(admin)
    db.commit()
    db.refresh(admin)
    return admin

@pytest.fixture
def normal_admin(db):
    """创建普通管理员"""
    admin = Admin(
        username="normaladmin",
        email="normal@example.com",
        hashed_password="$2b$12$LQv3c1yqBWVHxkd0LHAkCOYz6TtxMQJqhN8/LewKrNYZGpk1hPEUy",  # password: test123
        is_super_admin=False,
        is_active=True
    )
    db.add(admin)
    db.commit()
    db.refresh(admin)
    return admin

@pytest.fixture
def test_student(db):
    """创建测试学生"""
    student = Student(
        student_id="2021001",
        name="测试学生",
        email="student@example.com",
        hashed_password="$2b$12$LQv3c1yqBWVHxkd0LHAkCOYz6TtxMQJqhN8/LewKrNYZGpk1hPEUy",  # password: test123
        gender="male",
        grade=2021,
        major="计算机科学",
        is_active=True
    )
    db.add(student)
    db.commit()
    db.refresh(student)
    return student

@pytest.fixture
def test_building(db):
    """创建测试宿舍楼"""
    building = Building(
        name="1号楼",
        floors=6,
        rooms_per_floor=10,
        gender="male"
    )
    db.add(building)
    db.commit()
    db.refresh(building)
    return building

@pytest.fixture
def test_room(db, test_building):
    """创建测试房间"""
    room = Room(
        building_id=test_building.id,
        floor=1,
        room_number="101",
        capacity=4,
        current_occupancy=0
    )
    db.add(room)
    db.commit()
    db.refresh(room)
    return room

@pytest.fixture
def test_interest(db):
    """创建测试兴趣标签"""
    interest = Interest(
        name="编程",
        description="喜欢编程和开发"
    )
    db.add(interest)
    db.commit()
    db.refresh(interest)
    return interest 